package cn.qixin.passport.controller;

import cn.qixin.passport.service.IUploadService;
import cn.qixin.passport.web.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;
@Slf4j
@RestController
public class UploadController {
    //把配置文件中的值读取出
    @Autowired
    IUploadService uploadService;
    @Value("${dirPath}")
    private String dirPath;
    @PostMapping("/upload")
    public JsonResult<String> upload(@RequestParam("file") MultipartFile pic) throws IOException {
        String head = uploadService.upload(pic);
        log.debug(head);
        return JsonResult.ok(head);
    }

    @RequestMapping("/remove")
    public void remove(String url){
        //删除文件
        new File(dirPath+url).delete();
    }

}
